{% extends "elements/table_list.html" %}

{% load i18n %}
{% load markup %}
{% load introspection %}

{% block table_headers %}
{{ block.super }}

    {% block table_filterbox %}
    <tr class="filterbox">
        {% for h in table.headers %}
        {% join '_' table.uid 'filter_by' h.attname as filter_by_key %}
        {% join '_' table.uid 'filter_expr' h.attname as filter_expr_key %}
        {% block table_field_filter %}
        <td class="{{ h.attname }}-filter">
            {% if h.type == "boolean" %}
            <select title="{% trans 'Select a filter value' %}" name="{{ filter_by_key }}" />
                <option value=""{% if h.filter.value == None %} selected="selected"{% endif %}></option>
                <option value="1"{% if h.filter.value == "1" %} selected="selected"{% endif %}>{% trans "True" %}</option>
                <option value="0"{% if h.filter.value == "0" %} selected="selected"{% endif %}>{% trans "False" %}</option>
            </select>
            {% elif h.type == "foreignkey" %}
            <select title="{% trans 'Select a filter value' %}" name="{{ filter_by_key }}" />
                <option value=""{% if h.filter.value == None %} selected="selected"{% endif %}></option>
                {% with fk_name=h.attname|slice:":-3" %}
                {% for object in unfiltered_object_list %}
                {% with fkobj=object|get:fk_name %}
                {% with fk=fkobj.pk %}
                {% ifchanged fkobj %}
                <option value="{{ fk }}"{% if h.filter.value == fk|safe %} selected="selected"{% endif %}>{{ fkobj }}</option>
                {% endifchanged %}
                {% endwith %}
                {% endwith %}
                {% endfor %}
                {% endwith %}
            </select>
            {% else %}
            <select title="{% trans 'Select a filter criteria' %}" class="expr" name="{{ filter_expr_key }}" />
                <option value=""></option>
                <option value="iexact"{% if h.filter.expr == "iexact" or not h.filter.expr %} selected="selected"{% endif %}>&#61</option>
                <option value="gt"{% if h.filter.expr == "gt" %} selected="selected"{% endif %}>&gt</option>
                <option value="gte"{% if h.filter.expr == "gte" %} selected="selected"{% endif %}>&gt&#61</option>
                <option value="lt"{% if h.filter.expr == "lt" %} selected="selected"{% endif %}>&lt</option>
                <option value="lte"{% if h.filter.expr == "lte" %} selected="selected"{% endif %}>&lt&#61</option>
                <option value="icontains"{% if h.filter.expr == "icontains" %} selected="selected"{% endif %}>{% trans "Has" %}</option>
                <option value="istartswith"{% if h.filter.expr == "istartswith" %} selected="selected"{% endif %}>{% trans "Starts" %}</option>
                <option value="iendswith"{% if h.filter.expr == "iendswith" %} selected="selected"{% endif %}>{% trans "Ends" %}</option>
            </select>
            {% if h.type == "datetime" %}
            <input title="{% trans 'Type a filter date/time' %}" name="{{ filter_by_key }}" type="text" placeholder="{% trans 'YYYY-MM-DD HH:MM' %}" value="{{ h.filter.value }}" />
            {% else %}
            <input title="{% trans 'Type a filter pattern' %}" name="{{ filter_by_key }}" type="text" placeholder="{% trans '...' %}" value="{{ h.filter.value }}" />
            {% endif %}
            {% endif %}
        </td>
        {% endblock %}
        {% endfor %}
        <td>
            <ul class="actions">
                {% join '_' table.uid 'filter' as filter_button_name %}
                {% join '_' table.uid 'reset_filters' as reset_filters_button_name %}
                <li class="search"><button title="{% trans 'Filter' %}" name="{{ filter_button_name }}">{% trans "Filter" %}</button></li>
                <li class="list"><button title="{% trans 'Reset' %}" name="{{ reset_filters_button_name }}">{% trans "Reset" %}</button></li>
            </ul>
        </td>
    </tr>
    {% endblock %}
    
{% endblock %}

{% block table_first_column %}
{% if forloop.counter0 == 0 %}
<input title="{% trans 'Select this item' %}" name="{% if table.uid %}{{ table.uid }}_{% endif %}select_{{ row.object.pk }}" type="checkbox" />
<a title="{{ f }}" href="{{ row.object.get_absolute_url }}">{{ f }}</a>
{% else %}
{{ block.super }}
{% endif %}
{% endblock %}

{% block table_field_actions %}
<ul class="actions">
    {% if row.object.get_edit_url %}<li class="edit"><a title="{% trans 'Edit' %}" href="{{ row.object.get_edit_url }}">{% trans "Edit" %}</a></li>{% endif %}
    {% if row.object.get_delete_url %}<li class="delete"><a title="{% trans 'Delete' %}" href="{{ row.object.get_delete_url }}">{% trans "Delete" %}</a></li>{% endif %}
</ul>
{% endblock %}

{% block table_footer %}
{% if row_count %}
<tfoot>
    <tr>
        <td>
            <input title="{% trans 'Select all' %}" name="{% if table.uid %}{{ table.uid }}_{% endif %}select_all" type="checkbox" />
            <strong>{% blocktrans with row_count=table.rows|length total_count=paginator.count %}All ({{ row_count }} of {{ total_count }} items){% endblocktrans %}</strong>
        </td>
        {% if field_count > 1 %}
        <td colspan="{{ field_count|add:'-1' }}"></td>
        {% endif %}
        <td>
            {% block table_list_actions %}
            <ul class="actions">
                <li class="delete"><button title="{% trans 'Delete selected' %}" name="{% if table.uid %}{{ table.uid }}_{% endif %}delete_selected">{% trans "Delete" %}</button></li>
            </ul>
            {% endblock %}
        </td>
    </tr>
</tfoot>
{% else %}
{{ block.super }}
{% endif %}
{% endblock %}
